package com.lunwen.lunwen.controller;

import com.lunwen.lunwen.bean.BanJi;
import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.XueYuan;
import com.lunwen.lunwen.services.BanJiService;
import com.lunwen.lunwen.services.ZhuanYeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 * (BanJi)表控制层
 *
 * @author makejava
 * @since 2020-07-20 13:23:05
 */
@RestController
@RequestMapping("api")
public class BanJiController {

    /**
     * 服务对象
     */
    @Autowired
    private BanJiService banJiService;

    @Autowired
    private ZhuanYeService zhuanYeService;

    @RequestMapping("selectonebanji")
    public Result selectOneBanJi(@RequestParam("mingCheng") String mingCheng){
        System.out.println(mingCheng);
        Result result = new Result();
        result.setResult("200","查询成功！");
        result.setData(banJiService.queryByMingCheng(mingCheng));
        return result;
    }

    @RequestMapping("selectallbanji")
    public Result selectAllBanJi(@RequestParam("zhuanyeId") String zhuanyeId){
        Result result = new Result();
        if (StringUtils.isEmpty(zhuanyeId)){
            result.setResult("201","专业ID不能为空！");
            return result;
        }
        BanJi banJi = new BanJi();
        banJi.setZhuanyeId(zhuanyeId);
        result.setResult("200","查询成功！");
        result.setData(banJiService.queryAll(banJi));
        return result;
    }

    @RequestMapping("insertbanji")
    public Result insertBanJi(@RequestBody BanJi banJi){
        Result result = new Result();
        if (StringUtils.isEmpty(banJi.getBanjimingcheng())){
            result.setResult("201","班级名称不能为空！");
            return result;
        }
        if(banJiService.queryByMingCheng(banJi.getBanjimingcheng()) != null){
            result.setResult("201","该班级已存在！");
            return result;
        }
        if (StringUtils.isEmpty(banJi.getZhuanyeId())){
            result.setResult("201","专业ID不能为空！");
            return result;
        }
        if(zhuanYeService.queryById(banJi.getZhuanyeId()) == null){
            result.setResult("201","不存在该专业！");
            return result;
        }
        String id = UUID.randomUUID().toString();
        banJi.setId(id);
        banJiService.insert(banJi);
        result.setResult("200","新增成功！",id);
        return result;
    }

}